<?php
$page = 'add_app';
include 'connect/connect.php';
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO apps SET app_name='$app_name', app_url='$app_url',  added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?");
}


if (isset($_GET['update'])) {
    session_start();
    extract($_POST);
    $tid = $_GET['id'];
    $query = "UPDATE apps SET app_name='$app_name', app_url='$app_url'  WHERE id='$tid' ";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?");
}
?>
<?php require_once 'header.php'; ?>
<div id="content"><div>
        <h2>Welcome <?php echo $_SESSION['username']; ?></h2>

        <div id="edit_result"></div>
        <div id="emulator"></div>
        <div id="delete_result" style="display:none">
            <p class="error">App deleted!</p>
        </div>
        <script type="text/javascript">
            initDialog("#edit_result","Edit app");
            initDialog("#delete_result","App deleted");
            
            $(function() {
                $("#emulator").dialog({
                    autoOpen: false,
                    modal: false,
                    title: "Emulator",
                    width: 285,
                    height:543
                });
            });

            jQuery(document).ready(function(){
                jQuery("#app_form").validationEngine();
            });

            function editApp(tid){
                $("#edit_result").hide();
                $("#loading").show();
                //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
                $("#edit_result").load("edit.php?id="+tid,function() {
                    openDialog('#edit_result');
                    $("#edit_result").show();
                    $("#loading").hide();
                });
            }
            
            function emulate(tid){
                $("#emulator").hide();
                $("#loading").show();
                //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
                $("#emulator").load("emulator.php?appId="+tid,function() {
                    openDialog('#emulator');
                    $("#emulator").show();
                    $("#loading").hide();
                });
            }
    
    
            function deleteApp(tid){
                if(confirm("Are you sure you want to delete?")){
                    $("#loading").show();
                    //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
                    $("#edit_result").load("delete.php?id="+tid,function() {
                        openDialog('#delete_result');
                        $("#loading").hide();
                    });
                    window.location="?";
                }
            }
        </script>
        <img src="img/loading.gif" alt="Loading.." style="display: none;" id="loading"/>
        <form action="?save=1" method="POST" id="app_form">
            <table border='1'>
                <tr>
                    <td>Application Name</td>
                    <td><input type="text" class="validate[required] wide" name="app_name" id='app_name'/></td>

                    <td>Home page Url</td>
                    <td><input type="text" class="validate[required] wide" name="app_url" id='app_url' /></td>

                    <td colspan="2" style="text-align: right;"><input type="submit"/></td>
                </tr>
            </table>
        </form>

        <?php
        $username = $_SESSION['username'];
        $query = "SELECT * FROM apps where added_by='$username'";
        $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
        if ($result == 0) {
            echo "You have not uploaded any applications in W.A.R.E yet! Fill the form above to upload your first application.";
        } else {
            ?>

            <table border='1' >
                <tr>
                    <th>
                        Application name
                    </th>
                    <th>
                        Home page url
                    </th>
                    <th>
                    </th>
                    <th>
                    </th>
                </tr>
                <?php
                while ($row = mysql_fetch_assoc($result)) {
                    //var_dump($row);
                    extract($row);
                    ?>
                    <tr>
                        <td>
                            <?php
                            echo $app_name;
                            ?>
                        </td>
                        <td>
                            <?php
                            echo $app_url;
                            ?>
                        </td>
                        <td>
                            <a href="javascript:emulate(<?php echo $id ?>);">Emulate</a>
                            <a href="javascript:editApp(<?php echo $id ?>);"><img src="img/edit.png" alt="Edit" title="Edit"/></a>
                            <a href="javascript:deleteApp(<?php echo $id ?>);"><img src="img/delete.png" alt="Delete" title="Delete"/></a>
                        </td>
                        <td><a href="usage.php?appId=<?php echo $id ?>">Usage Statistics</a></td>

                    </tr>
                    <?php
                }
            }
            ?>
        </table>


        <?php require_once('footer.php'); ?>